//
//  AppDelegate.swift
//  iOSWeibo12
//
//  Created by Admin on 16/9/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        setupAppearance()
        
        HMSQLiteManager.shared
        
        // TODO: 加载账号,能加载到账号表示用户登录了,加载不到账号表示用户没有登录
        let userAccount = HMUserAccountViewModel.shared.userAccount
        print("AppDelegate加载账号: userAccount: \(userAccount)")
        
        window = UIWindow(frame: UIScreen.main.bounds)
        
//        let tabbarVC = HMMainTabbarController()
        window?.rootViewController = defaultController()
        
        // 测试
//        window?.rootViewController = HMWelcomeViewController()
//        window?.rootViewController = HMNewFeatureViewController()
        
        window?.makeKeyAndVisible()
        
        return true
    }
    
    /// 给外面的人调用来切换界面
    class func outSwitchRootViewController(controller: UIViewController) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        appDelegate.window?.rootViewController = controller
    }
    
    
    // MARK: - 程序启动后默认的控制器
    private func defaultController() -> UIViewController {
        // 没有登录直接返回MainViewController
        if !HMUserAccountViewModel.shared.userLogin {
            return HMMainTabbarController()
        }
        
        // 判断是否是新版本
        return isNewVersion() ? HMNewFeatureViewController() : HMWelcomeViewController()
    }
    
    /// 判断是否是新版本
    private func isNewVersion() -> Bool {
        // 获取程序当前的版本
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        
        // 获取上次运行的版本
        let key = "lastVersion"
        let lastVersion = UserDefaults.standard.string(forKey: key)
        
        let isNewVersion = currentVersion != lastVersion
        // 比较
        if isNewVersion {
            // 新版本
            // 保存新版本
            UserDefaults.standard.set(currentVersion, forKey: key)
            UserDefaults.standard.synchronize()
        }
        
        // 返回
        return isNewVersion
    }
    
    /// 设置全局外观
    private func setupAppearance() {
        SVProgressHUD.setMinimumDismissTimeInterval(2)
        UINavigationBar.appearance().tintColor = UIColor.orange
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        HMStatusDAL.shared.clearCacheData()
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

